package cn.hznc.controller.service.order.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hznc.constant.CommonConstant;
import cn.hznc.domain.entity.*;
import cn.hznc.vo.BomParamVo;
import cn.hznc.vo.CraftParamVo;
import com.alibaba.fastjson.JSONArray;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 工单业务模型
 *
 * @Author Administrator
 * @Date 2023/3/21 9:46
 */
@Getter
@Setter
public class OrderDo {
    private OrderCraftEntity orderCraftEntity;
    private OrderBomEntity orderBomEntity;
    private OrderEntity orderEntity;


    private List<ProcessCraftEntity> processCraftEntityList;

    private List<ProductionBomEntity> productionBomEntityList;


    public OrderDo() {

    }

    public OrderDo(OrderEntity orderEntity, List<ProcessCraftEntity> processCraftEntityList, List<ProductionBomEntity> productionBomEntityList) {
        this.orderEntity = orderEntity;
        this.processCraftEntityList = processCraftEntityList;
        this.productionBomEntityList = productionBomEntityList;
        this.orderBomEntity = buildOrderBom(productionBomEntityList, orderEntity);
        this.orderCraftEntity = buildOrderCraft(processCraftEntityList, orderEntity);
    }


    private OrderBomEntity buildOrderBom(List<ProductionBomEntity> productionBomEntityList, OrderEntity orderEntity) {
        OrderBomEntity orderBomEntity = new OrderBomEntity();
        orderBomEntity.setFigureNo(orderEntity.getFigureNo());
        if (CollectionUtil.isEmpty(productionBomEntityList)) {
            orderBomEntity.setBomParam(JSONArray.toJSONString(new ArrayList<>()));
            return orderBomEntity;
        }
        List<BomParamVo> bomParamVos = productionBomEntityList.stream().map(this::fillBomParamVo).collect(Collectors.toList());
        orderBomEntity.setBomParam(JSONArray.toJSONString(bomParamVos));
        return orderBomEntity;
    }

    private BomParamVo fillBomParamVo(ProductionBomEntity productionBomEntity) {
        BomParamVo bomParamVo = new BomParamVo();
//        bomParamVo.setId(productionBomEntity.getId());
//        bomParamVo.setCode(productionBomEntity.getCode());
//        bomParamVo.setDescription(productionBomEntity.getDescription());
//        bomParamVo.setName(productionBomEntity.getName());
//        bomParamVo.setVersion(productionBomEntity.getVersion());
//        bomParamVo.setSeq(productionBomEntity.getSeq());
//        bomParamVo.setQuantity(productionBomEntity.getQuantity());
        BeanUtils.copyProperties(productionBomEntity , bomParamVo);
        return bomParamVo;
    }


    private OrderCraftEntity buildOrderCraft(List<ProcessCraftEntity> processCraftEntityList, OrderEntity orderEntity) {
        OrderCraftEntity orderCraftEntity = new OrderCraftEntity();
        orderCraftEntity.setFigureNo(orderEntity.getFigureNo());
        if (CollectionUtil.isEmpty(processCraftEntityList)) {
            orderCraftEntity.setCraftParam(JSONArray.toJSONString(new ArrayList<>()));
        }
        List<CraftParamVo> craftParamVos = processCraftEntityList.stream().map(processCraftEntity -> fillCraftParamVo(processCraftEntity , orderEntity.getPlanNumber())).collect(Collectors.toList());
        orderCraftEntity.setCraftParam(JSONArray.toJSONString(craftParamVos));
        return orderCraftEntity;
    }

    private CraftParamVo fillCraftParamVo(ProcessCraftEntity processCraftEntity , Integer planNumber) {
        CraftParamVo craftParamVo = new CraftParamVo();
        craftParamVo.setCraftPlanNumber(planNumber);
        craftParamVo.setCraftUnallocatedNumber(planNumber);
        craftParamVo.setAdjustNumber(ObjectUtil.isNull(processCraftEntity.getAdjust()) ? 0 : processCraftEntity.getAdjust());
        craftParamVo.setCraftSingleTimeConsuming(ObjectUtil.isNull(processCraftEntity.getSingleTimeConsuming()) ? new BigDecimal("0.00") : processCraftEntity.getSingleTimeConsuming());
        craftParamVo.setId(processCraftEntity.getId());
        craftParamVo.setCode(processCraftEntity.getCode());
        craftParamVo.setName(processCraftEntity.getName());
        craftParamVo.setContent(processCraftEntity.getContent());
        craftParamVo.setSeq(processCraftEntity.getSeq());
        craftParamVo.setProgramCode(processCraftEntity.getProgramCode());
        craftParamVo.setStatus(processCraftEntity.getProcessesStatus());
        craftParamVo.setStartTime(processCraftEntity.getStartTime());
        craftParamVo.setEndTime(processCraftEntity.getEndTime());
        craftParamVo.setPlanTime(processCraftEntity.getPlanTime());
        craftParamVo.setInboundNumber(processCraftEntity.getInboundNumber());
        craftParamVo.setRejectedNumber(processCraftEntity.getRejectedNumber());
        craftParamVo.setInProcessNumber(processCraftEntity.getInProcessNumber());
        craftParamVo.setQualifiedNumber(processCraftEntity.getQualifiedNumber());
        craftParamVo.setTypeFlag(0);
        return craftParamVo;
    }
}
